package com.design3.graduatethesissys.service.impl;

import com.design3.graduatethesissys.domain.Message;
import com.design3.graduatethesissys.domain.SysUser;
import com.design3.graduatethesissys.mapper.MessageMapper;
import com.design3.graduatethesissys.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;
    //根据当前用户id查询相关记录
    @Override
    public SysUser findMessageById(String id) {
        return messageMapper.findByUserName(id);
    }
    //保存信息
    @Override
    public void insertMessage(Message message) {
        messageMapper.insertMessage(message);
    }
    //根据当前用户ID查询出所有的未读消息
    @Override
    public List<Message> getMyNewMessage(String myId) {
        List<Message> messages = messageMapper.getMyNewMessage(myId);
        return messages;
    }
    //根据对方ID和自己ID查询出和自己相关的前十条消息，并且把发給自己的消息置为已读，用于初始化窗口展现历史记录
    @Override
    public List<Message> getMessageByFromId(String myId, String yourId) {
        List<Message> messages = messageMapper.getMessageByFromId(myId,yourId);
        if(!messages.isEmpty()){
            messageMapper.updateMyMessages(messages,myId);
        }
        return messages;
    }
    //根据对方获取到发給自己的默认的未读消息，默认前十条，用于判断有没有新消息并展示
    @Override
    public List<Message> getNewMessageById(String myId, String yourId) {
        List<Message> messages =  messageMapper.getNewMessageById(myId,yourId);
        /*if(!messages.isEmpty()){
            messageMapper.updateStatus(messages);
        }*/
        return messages;
    }
    //根据对方获取所有发給自己的未读消息，并更新为已读，用于每次发送消息后更新所有自己相关消息为已读
    @Override
    public void updateOldMessage(String myId,String yourId) {
        List<Message> messages =  messageMapper.getAllNewMessageById(myId,yourId);
        if(!messages.isEmpty()){
            messageMapper.updateStatus(messages);
        }
    }


}
